﻿@using EmployeeLeaveSystem.Shared.DAO;
@using EmployeeLeaveSystem.Shared.Models;
@inject INotificationService _notice
@inject IMessageService _message
@inject HttpClient _httpClient;
@inject IJSRuntime Js;
@inject NavigationManager _NavigationManager;
<Router AppAssembly="@typeof(App).Assembly">
    <Found Context="routeData">
       <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        <!--<ReuseTabsRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />-->
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
    <NotFound>
        <PageTitle>Not found</PageTitle>
        <LayoutView Layout="@typeof(MainLayout)">
            <p role="alert">资源丢失了 ErrCode 404</p>
        </LayoutView>
    </NotFound>
</Router>
<AntContainer />

<Modal Visible="@showModalMsg"
       Title="@modalTitle"
       OnCancel="()=>{showModalMsg=false;}"
       Footer="@null"
       Draggable="true">
    <Form Model="@commentData" @ref="@msgForm" OnFinish="()=>{PostMsg();}">
        <FormItem>
            <Text>回复消息：</Text>
            <TextArea @bind-Value="@context.Msg"></TextArea>
        </FormItem>
        <FormItem>
            <p>@DateTime.Now</p>
            <Button Type="@ButtonType.Primary" HtmlType="submit" @ref="@submit">
                递交
            </Button>
        </FormItem>
    </Form>
</Modal>

@code {
    public static App? _appHandle;
    public App() {
        _appHandle = this;
        GobalManager gobalManager = new(_httpClient, _NavigationManager);
    }
    public async Task NoticeWithIcon(NotificationType type, string Title = "", string Body = "") {
        await _notice.Open(new NotificationConfig() {
                Message = Title,
                Description = Body,
                NotificationType = type
            });
    }

    public async Task GetAllStaffInfos() {
        var res = await _httpClient.GetFromJsonAsync<NetworkDataLocal<List<StaffInfo>>>(@"Main/GetAllStaffInfos");
        if (res == null) { Console.WriteLine("网络流信息获取失败"); return; }
        if (res.data == null) {
            Console.WriteLine("获取StaffInfo的信息失败了\n" + res.errMsg);
        } else {
            GobalManager.staffInfos = res.data;
            //Console.WriteLine("获取员工列表信息成功" + GobalManager.staffInfos.Count);
        }
    }

    public async Task GetUserHistory() {
        if (GobalManager.myInfo == null) return;
        var res = await _httpClient.GetFromJsonAsync<NetworkDataLocal<StaffInfo>>($"Main/GetHistoryByUserId?id={GobalManager.myInfo.Id}");
        GobalManager.myInfo.LeaveDetail = res.data.LeaveDetail;
    }

    public async void BackPage() {
        await Js.InvokeVoidAsync("BackPage");
    }

    public void ToUrl(string url) {
        _NavigationManager.NavigateTo(url);
    }
    public Modal msgModal;
    public Form<Comments> msgForm;
    public Button? submit;
    public bool showModalMsg = false;
    public string modalTitle = "递交销假申请";
    public Comments commentData = new();
    EventCallback<MouseEventArgs> OnMinutesAdded { get; set; }
    int opt = 0;// 0：递交普通消息 1：递交销假 2：递交同意 3：递交回绝消息
    int leaveId = 0; // 要操作的消息ID
    public async void XiaoJiaLeaveDetail(int id) { // 销假申请
        modalTitle = "递交销假申请，请填写原因：";
        showModalMsg = true;
        StateHasChanged();
        if (GobalManager.myInfo == null) { _message.Error("您尚未登录"); return; }
        commentData.StaffInfoId = GobalManager.myInfo.Id;
        commentData.LeaveDetailId = id;
        commentData.Type = CommentType.Finished; //!!!
        opt = 1;
        leaveId = id;
    }
    public async void PostMsgToIdLeaveDetail(int id) { // 普通消息递交
        modalTitle = "发送一条普通消息：";
        showModalMsg = true;
        StateHasChanged();
        if (GobalManager.myInfo == null) { _message.Error("您尚未登录"); return; }
        commentData.StaffInfoId = GobalManager.myInfo.Id;
        commentData.LeaveDetailId = id;
        commentData.Type = CommentType.Noraml; //!!!
        opt = 0;
        leaveId = id;
    }
    public async void AcceptMsgToIdLeaveDetail(int id) { // 普通消息递交
        modalTitle = "批准本条请假消息：";
        commentData.Msg = "我批准本条请假信息";
        showModalMsg = true;
        StateHasChanged();
        if (GobalManager.myInfo == null) { _message.Error("您尚未登录"); return; }
        commentData.StaffInfoId = GobalManager.myInfo.Id;
        commentData.LeaveDetailId = id;
        commentData.Type = CommentType.Accept; //!!!
        opt = 2;
        leaveId = id;
    }
    public async void RejectMsgToIdLeaveDetail(int id) { // 普通消息递交
        modalTitle = "拒绝本条请假消息：";
        commentData.Msg = "本条请假消息不合格，我拒绝";
        showModalMsg = true;
        StateHasChanged();
        if (GobalManager.myInfo == null) { _message.Error("您尚未登录"); return; }
        commentData.StaffInfoId = GobalManager.myInfo.Id;
        commentData.LeaveDetailId = id;
        commentData.Type = CommentType.Reject; //!!!
        opt = 3;
        leaveId = id;
    }
    private async void PostMsg() {
        // 发送消息事件
        await _httpClient.PostAsJsonAsync($"Main/PostComment", commentData);
        // 递交事件状态更改
        if (opt == 0) {
            showModalMsg = false;
            StateHasChanged();
            NoticeWithIcon(NotificationType.Success, "网络状态更改", "发送成功");
        } else if (opt == 1) { //递交请假销毁消息
            await _httpClient.PostAsJsonAsync($"Main/UpdateLeaveDetailAcceptFlag", new UpdateLeaveAcceptFlag() { Id = leaveId, AcceptFlag = AcceptFlag.正常结束 });
            showModalMsg = false;
            StateHasChanged();
            NoticeWithIcon(NotificationType.Success, "网络状态更改", "已经成功销假了");
        } else if (opt == 2) { // 递交同意消息
            await _httpClient.PostAsJsonAsync($"Main/UpdateLeaveDetailAcceptFlag", new UpdateLeaveAcceptFlag() { Id = leaveId, AcceptFlag = AcceptFlag.接受 });
            showModalMsg = false;
            StateHasChanged();
            NoticeWithIcon(NotificationType.Success, "此消息已经更改", "您已经批准了申请");
        } else if (opt == 3) { // 递交回绝消息
            await _httpClient.PostAsJsonAsync($"Main/UpdateLeaveDetailAcceptFlag", new UpdateLeaveAcceptFlag() { Id = leaveId, AcceptFlag = AcceptFlag.驳回 });
            showModalMsg = false;
            StateHasChanged();
            NoticeWithIcon(NotificationType.Success, "此消息已经更改", "您拒绝了这个消息的申请");
        }
        GobalManager.FlushPages();
    }
    public IMessageService GetMessageShow() {
        return _message;
    }
}